package org.fpp.sqlparse;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author fpp
 * @version 1.0
 * @since 2020/6/2 17:41
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@ToString
public class TableInfo implements Serializable {
    private String name;
    private String comment;
    private List<ColumnInfo> columnList;
    private List<IndexInfo> indexList;

    public void addColumnInfo(ColumnInfo columnInfo){
        if (columnList == null) {
            columnList = new ArrayList<>();
        }
        columnList.add(columnInfo);
    }

    public void addColumnInfo(ColumnInfo... columnInfos){
        if (columnList == null) {
            columnList = new ArrayList<>();
        }
        columnList.addAll(Arrays.asList(columnInfos));
    }

    public void addIndexInfo(IndexInfo indexInfo){
        if (indexList == null) {
            indexList = new ArrayList<>();
        }
        indexList.add(indexInfo);
    }

    public void addIndexInfo(IndexInfo... indexInfos){
        if (indexList == null) {
            indexList = new ArrayList<>();
        }
        indexList.addAll(Arrays.asList(indexInfos));
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @EqualsAndHashCode
    @ToString
    public static class ColumnInfo implements IndexSerializable{
        //序号
        private Integer index;
        private String name;
        private String comment;
        private String dataType;
        private String defaultValue;
        private Integer length;
        /**
         * 是否能够为空 true表示可控 false表示非空
         */
        private Boolean nullable;
        private Boolean primaryKey;
        private Boolean uniqueKey;
        private Boolean autoIncrement;

        public static ColumnInfo defaultObj(){
            ColumnInfo columnInfo=new ColumnInfo();
            columnInfo.setNullable(true);
            columnInfo.setPrimaryKey(false);
            columnInfo.setUniqueKey(false);
            columnInfo.setAutoIncrement(false);
            return columnInfo;
        }
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @EqualsAndHashCode
    @ToString
    public static class IndexInfo implements IndexSerializable{
        //序号
        private Integer index;
        private String name;
        private String comment;
        private List<String> columnNames;
        private Boolean uniqueKey;
        public static IndexInfo defaultObj(){
            IndexInfo indexInfo=new IndexInfo();
            indexInfo.setUniqueKey(false);
            return indexInfo;
        }

        public void addColumn(String columnName){
            if(null==columnNames){
                columnNames=new ArrayList<>();
            }
            columnNames.add(columnName);
        }

        public void addColumn(String... columnName){
            if(null==columnNames){
                columnNames=new ArrayList<>();
            }
            columnNames.addAll(Arrays.asList(columnName));
        }
    }
}